package com.demo.controller;


import com.demo.annotation.GlobalInterceptor;
import com.demo.annotation.VerifyParam;
import com.demo.entity.constants.Constants;
import com.demo.entity.dto.SessionUserLoginDto;
import com.demo.entity.enums.NoticeStatusEnum;
import com.demo.entity.enums.NoticeTypeEnum;
import com.demo.entity.enums.PermissionCodeEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.NoticeInfo;
import com.demo.entity.query.NoticeInfoQuery;
import com.demo.entity.vo.PaginationResultVO;
import com.demo.entity.vo.ResponseVO;
import com.demo.exception.BusinessException;
import com.demo.service.NoticeInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController("noticeInfoController")
@RequestMapping("/noticeInfo")
public class NoticeInfoController extends ABaseController {

	@Resource
	private NoticeInfoService noticeInfoService;


	/**
	 * 分页查询
	 */
	@RequestMapping("/loadNoticeList")
	@GlobalInterceptor(permissionCode = PermissionCodeEnum.NOTICE_LIST)
	public ResponseVO loadDataList(HttpSession session,NoticeInfoQuery query) {
		query.setOrderBy("publish_time desc");
		query.setType(NoticeTypeEnum.NOTICE.getType());
		return getSuccessResponseVO(noticeInfoService.findListByPage(query));
	}


	/**
	 * 保存通知
	 * @return
	 */
	@RequestMapping("/saveNotice")
	@GlobalInterceptor(permissionCode = PermissionCodeEnum.NOTICE_EDIT)
	public ResponseVO saveNotice(HttpSession session,
			NoticeInfo bean) {
		SessionUserLoginDto webUserDto = getUserAdminFromSession(session);
		bean.setUserId(webUserDto.getUserId());
		this.noticeInfoService.saveNotice(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id查询
	 */
	@RequestMapping("/getNoticeInfoById")
	@GlobalInterceptor(permissionCode = PermissionCodeEnum.NOTICE_LIST)
	public  ResponseVO getNoticeInfoById(@VerifyParam(required = true) Integer id) {
		return getSuccessResponseVO(this.noticeInfoService.getNoticeInfoById(id));
	}

	/**
	 * 修改通知状态
	 * @return
	 */
	@RequestMapping("/updateStatus")
	@GlobalInterceptor(permissionCode = PermissionCodeEnum.NOTICE_EDIT)
	public ResponseVO updateStatus(HttpSession session,
									@VerifyParam(required = true) Integer id,
								   @VerifyParam(required = true) Integer status) {
		SessionUserLoginDto webUserDto = getUserAdminFromSession(session);
		this.noticeInfoService.updateStatus(id,status,webUserDto.getUserId());
		return getSuccessResponseVO(null);
	}

	/**
	 * 删除通知
	 */
	@RequestMapping("/deleteNoticeById")
	@GlobalInterceptor(permissionCode = PermissionCodeEnum.NOTICE_DEL)
	public  ResponseVO deleteNoticeById(@VerifyParam(required = true) Integer id,
										HttpSession session) {
		SessionUserLoginDto webUserDto = getUserAdminFromSession(session);
		return getSuccessResponseVO(this.noticeInfoService.deleteNoticeInfoById(id,webUserDto.getUserId()));
	}

	/**
	 * 分页查询
	 */
	@RequestMapping("/loadNotice4User")
	@GlobalInterceptor(permissionCode = PermissionCodeEnum.NOTICE_LIST)
	public ResponseVO loadNotice4User(HttpSession session,NoticeInfoQuery query) {
		if (query.getType()==null){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		NoticeTypeEnum noticeTypeEnum = NoticeTypeEnum.getMenuTypeByType(query.getType());
		if (noticeTypeEnum==null){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		if (noticeTypeEnum==NoticeTypeEnum.NOTICE){
			query.setStatus(NoticeStatusEnum.PUBLISHED.getStatus());
			query.setOrderBy("publish_time desc");
			PaginationResultVO<NoticeInfo> noticeInfos = noticeInfoService.findListByPage(query);
			return getSuccessResponseVO(noticeInfos);
		}
		if (noticeTypeEnum==NoticeTypeEnum.MESSAGE){
			SessionUserLoginDto webUserDto = getUserAdminFromSession(session);
			query.setUserId(webUserDto.getUserId());
			query.setOrderBy("publish_time desc");
			List<NoticeInfo> noticeInfos = noticeInfoService.findListByParam(query);
			return getSuccessResponseVO(noticeInfos);
		}
		return getSuccessResponseVO(null);
	}
}